<template @keydown.enter="submitForm('userAccount')">
<el-form :model="user" :rules="rules" ref="userAccount" class="Account">
    <el-form-item prop="username">
        <el-input v-model="user.username"   maxlength="11"
  show-word-limit  placeholder="请输入账号或手机号码" prefix-icon="el-icon-user"></el-input>
    </el-form-item>
    <el-form-item prop="password">
        <el-input type="password" v-model="user.password" placeholder="请输入密码" prefix-icon="el-icon-goods" clearable @keyup.enter="submitForm('userAccount')"></el-input>
    </el-form-item>
    <el-button type="primary" ref="login" class="btn-block" @click="submitForm('userAccount')" @keyup.enter="submitForm('userAccount')">登录</el-button>
</el-form>
</template>

<script>
export default {
    name: "Account",
    data() {
        return {
            user: {
                username: "",
                password: "",
            },
            rules: {
                username: [{
                        required: true,
                        message: "登录帐号不能为空",
                        trigger: "blur"
                    },
                    {
                        min: 3,
                        max: 15,
                        message: "长度在 3 到 15 个字符",
                        trigger: "blur",
                    },
                ],
                password: [{
                        required: true,
                        message: "密码不能为空",
                        trigger: "blur"
                    },
                    {
                        min: 3,
                        max: 15,
                        message: "长度在 3 到 15 个字符",
                        trigger: "blur",
                    },
                ],
            },
        };
    },
    mounted() {
        // 测试阶段.自动登录
        // this.submitForm("userAccount");
    },
    methods: {
        login(path = "Map") {
            this.user.grant_type = "password"
            this.$common
                .oauthLogin(this.user)
                .then((res) => {
                    if (res.access_token) {
                        this.$store.commit('ShowMenu/setShowMenu',this.$router.options.routes[0].children)
                        this.$message({
                            type: "success",
                            message: "登录成功",
                            duration: 300,
                        });
                        setTimeout(() => {
                            this.$router.push({
                                name: path,
                            });
                        }, 0);
                    }
                })
                .catch((err) => {
                    console.error(err);
                    // let tip = ''
                    // if (err.response.data.error === 'unauthorized') {
                    //     tip = ' 帐号或密码错误';
                    // }
                    this.$message({
                        type: "error",
                        message: '帐号或密码错误',
                        duration: 3000,
                    });
                });
        },
        submitForm(formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    this.login();
                } else {
                    return false;
                }
            });
        },
    },
};
</script>

<style lang="scss">
.Account {}
</style>
